import {UserDto} from "@/api/web/UserApi.ts";

export interface ILocalStore<T> {
    _key: string
    get: () => T | null
    set: (data: T) => void
}

// 基于JSON对象的存储逻辑，默认使用，继承并覆盖 _key 属性
export class JsonClazzStore<T> implements ILocalStore<T> {
    _key: string;

    constructor(key: string) {
        this._key = key
    }

    get(): T | null {
        const data = localStorage.getItem(this._key)
        if (data == null || data == "") {
            return null;
        }
        return JSON.parse(data);
    }

    set(data: T): void {
        localStorage.setItem(this._key, JSON.stringify(data))
    }

    remove(): void {
        localStorage.removeItem(this._key)
    }
}

export interface IUserStoreData {
    userDto: UserDto | null
}

export interface IThemeStoreData {
    type: "light" | "dark" | "default",
    color: string
}

export interface IBackgroundImageStoreData {
    src: string
}

export interface IRainingStoreData {
    raining: boolean
}

export interface IBgTransparencyStoreData {
    bgTransparency: number
}

export const userStoreLocal = new JsonClazzStore<IUserStoreData>("userStore")
export const themeStoreLocal = new JsonClazzStore<IThemeStoreData>("theme")
export const backgroundImageStoreLocal = new JsonClazzStore<IBackgroundImageStoreData>("bgImage")
export const rainingStoreLocal = new JsonClazzStore<IRainingStoreData>("raining")
export const bgTransparencyStoreLocal = new JsonClazzStore<IBgTransparencyStoreData>("bgTransparency")
